package com.example.psysystemhomework.alert;

import android.app.AlertDialog;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.psysystemhomework.LoginActivity;
import com.example.psysystemhomework.R;
import com.example.psysystemhomework.adapter.ReplyAdapter;
import com.example.psysystemhomework.bean.UserBean;
import com.example.psysystemhomework.util.AppManager;
import com.example.psysystemhomework.util.ShareUtil;

public class MyInformationAlert extends AlertDialog {
    ShareUtil shareUtil;
    private ImageView mLogo;
    private TextView mName;
    private TextView mSex;
    private TextView mAge;
    private TextView mPhone;
    private TextView mUserId;
    private TextView mEmo;

    public MyInformationAlert(Context context) {
        super(context);
        shareUtil = new ShareUtil(context);
    }

    protected MyInformationAlert(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected MyInformationAlert(Context context, int themeResId) {
        super(context, themeResId);
    }


    public void showAlert() {
        if (shareUtil.getUser() != null) {
            UserBean user = shareUtil.getUser();
            View inflate = View.inflate(getContext(), R.layout.my_information, null);
            this.setView(inflate);
            initViews(inflate);
            Glide.with(getContext()).load(user.getIconBean().getUrl()).into(mLogo);
            mName.setText(user.getName() + "");
            mSex.setText(user.getSex() == 0 ? "女" : "男");
            mAge.setText(user.getAge() + "");
            mPhone.setText(user.getPhone().length() > 10 ? user.getPhone().substring(0, 5) + "..." : user.getPhone());
            mUserId.setText(user.getUserId() + "");
            mEmo.setText("状态极佳");
            show();

        } else {
            this.setMessage("请先登录");
            this.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    AppManager appManager = AppManager.getAppManager();
                    appManager.addActivity(new LoginActivity());
                    Intent intent = new Intent(getContext(), appManager.currentActivity().getClass());
                    getContext().startActivity(intent);
                    show();
                }
            });

        }

    }

    private void initViews(View view) {
        mLogo = view.findViewById(R.id.logo);
        mName = view.findViewById(R.id.name);
        mSex = view.findViewById(R.id.sex);
        mAge = view.findViewById(R.id.age);
        mPhone = view.findViewById(R.id.phone);
        mUserId = view.findViewById(R.id.user_id);
        mEmo = view.findViewById(R.id.emo);
    }
}
